//
//  UserLongGestureController.m
//  Arts1
//
//  Created by apple on 15/9/16.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "UserLongGestureController.h"
#import "PhotoChooseController.h"
#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CoreAnimation.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "SetPhotoViewController.h"
#import "AllHeader.h"


@interface UserLongGestureController ()

/**
 *  添加背景模糊视图
 */
@property (nonatomic ,strong) UIVisualEffectView *visualEffectView;
/**
 *  选择拍照或相册
 */
@property (nonatomic ,strong) PhotoChooseController *photoChooseVC;

//图片发布页面
@property (nonatomic ,strong) SetPhotoViewController *setPhotoVC;
@end

@implementation UserLongGestureController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createVisualEffectView];
    
    [self creactTableView];
    
    [self creactBackView];
    
}

//回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

/**
 *  创建相册或相机页面选择页面
 */
- (void)creactTableView
{
    _photoChooseVC = [[PhotoChooseController alloc] initWithStyle:(UITableViewStylePlain)];
    _photoChooseVC.view.frame = CGRectMake(53 * WIDTH, 230 * HEIGHT, 270 * HEIGHT, 132 * HEIGHT);
    _photoChooseVC.view.layer.cornerRadius = 10;
    _photoChooseVC.tableView.scrollEnabled = NO;
    [UIView viewShadowColor:_photoChooseVC.view];
    [self.view addSubview:_photoChooseVC.view];


    
    __weak UserLongGestureController *wUserLGVC = self;
    _photoChooseVC.choosephotoblock = ^(NSInteger num){
    
        if (num == 0)
        {
            [wUserLGVC selectExistingPictureOrVideo];
        } else {
            [wUserLGVC shootPiicturePrVideo];
        }
    };
}

//创建发布视图
- (void)creactGetPhotoView
{
    _setPhotoVC = [[SetPhotoViewController alloc] init];
    _setPhotoVC.view.frame = CGRectMake(53 * WIDTH, 130 * HEIGHT, SCREEN_WIDTH - 106 * HEIGHT, 345 * HEIGHT);
    [UIView viewWideForShadowColor:_setPhotoVC.view];
    __weak UserLongGestureController *selfView = self;
    
    //移除视图
    _setPhotoVC.removeView = ^(void)
    {
        [selfView backView];
    };
    
    _setPhotoVC.view.layer.cornerRadius = 10;
    [self.view addSubview:_setPhotoVC.view];
}

//移除当前页面
- (void)creactBackView
{
    UIButton *backViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    backViewButton.frame = CGRectMake(SCREEN_WIDTH / 2.0 - 20, SCREEN_HEIGHT - 100 *HEIGHT, 40, 40);
    backViewButton.alpha = 0.3;
    [backViewButton setBackgroundImage:[UIImage imageNamed:@"cancelcon"] forState:(UIControlStateNormal)];
    
    [backViewButton addTarget:self action:@selector(backView) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backViewButton];
}
//取消当前页面
- (void)backView
{
    // 通过响应者链找到要操作的对象，直接进行事件的操作，通常是找到目标controller进行push，present，pop，dismiss 这些操作，也可以修改目标controller的属性，以及调用方法，传值
    UIResponder *responder = [self.view nextResponder];
    while (responder)
    {
        if ([responder isKindOfClass:[UserViewController class]])
        {
            [self.view removeFromSuperview];
            UserViewController *userVC = (UserViewController *)responder;
            userVC.navigationController.navigationBar.hidden = NO;
            userVC.tabBarController.tabBar.hidden = NO;
            [userVC.userCollection.collectionView reloadData];
            if (_delegate && [_delegate respondsToSelector:@selector(UserLongGestureControllerWithReLoadData)]) {
                [_delegate UserLongGestureControllerWithReLoadData];
            }
        }
        responder = [responder nextResponder];
    }
}

// 毛玻璃效果
- (void)createVisualEffectView
{
    UIVisualEffect *lightBlurEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)];
    _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:lightBlurEffect];
    _visualEffectView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _visualEffectView.alpha = 1;
    [self.view addSubview:_visualEffectView];
}


//从相机上选择
- (void)shootPiicturePrVideo
{
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}

//从相册中选择
- (void)selectExistingPictureOrVideo
{
    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}

//用户选中图片后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    //获得编辑过的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    //创建发布视图
    [self creactGetPhotoView];
    
    _setPhotoVC.image.image = image;
    
    [_photoChooseVC.view removeFromSuperview];
    
    //如果不选择的话，可以返回原来的页面
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (UIImage *)scaleFromImage:(UIImage *)image CGSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:(CGRectMake(0, 0, size.width, size.height))];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
// 用户选择取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //如果不选择的话，可以返回原来的页面
    [picker dismissViewControllerAnimated:NO completion:nil];
    
}


//根据用户选取的类型(相册或者相机)来判断当前状态是否支持该功能
-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
    NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    
    if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0)
    {
        NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        
        UIImagePickerController *picker=[[UIImagePickerController alloc] init];
        
        picker.mediaTypes = mediatypes;
        
        picker.delegate = self;
        
        picker.allowsEditing = YES;
        
        picker.sourceType = sourceType;
        
        NSString *requiredmediatype = (NSString *)kUTTypeImage;
        
        NSArray *arrmediatypes = [NSArray arrayWithObject:requiredmediatype];
        
        [picker setMediaTypes:arrmediatypes];
        
        [self presentViewController:picker animated:YES completion:^{
            
        }];
    }
    else
    {
        //如果不支持拍摄功能就弹出提示
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
